<?php
class Administrator_Model_Product_View extends Administrator_Model_Product_Model {
	public function __construct() {
		$this->db = Zend_Registry::get ( 'vicdbsystem' );
		$this->lang = new Nguyen_language_system ();
	}
	//product

	public function GetAllProduct(){
		$sql = 'select * from pro_details order by createdate DESC';
		return $this->db->fetchAll($sql);
	}
	public function GetAllProductByCat($cat){
		$sql = 'select * from pro_details where cat='.$cat.'  order by createdate DESC';
		return $this->db->fetchAll($sql);
	}
	public function GetAllProductByCatCount($cat,$count){
		$sql = 'select * from pro_details where cat='.$cat.'  order by createdate DESC limit 0,'.$count;
		return $this->db->fetchAll($sql);
	}
	public function GetAllProductByCatPage($cat,$page,$size){
		$sql = 'select * from pro_details where cat='.$cat.'  order by createdate DESC limit '.(($page-1)*$size).','.$size;
		return $this->db->fetchAll($sql);
	}
	public function GetRelatedProductIncat($id,$count){
		$info = $this->GetProductInfo($id);
		$sql = 'select * from pro_details where cat='.$info['cat'].' and id!='.$id.'  order by createdate DESC limit 0,'.$count;
		return $this->db->fetchAll($sql);
	}
	public function GetRelatedProductInType($id,$count){
		$info = $this->GetProductInfo($id);
		$sql = 'select * from pro_details where type='.$info['type'].' and id!='.$id.'  order by createdate DESC limit 0,'.$count;
		return $this->db->fetchAll($sql);
	}
	public function updateProducthit($id) {
		$info = $this->GetProductInfo($id);
		$hit = $info['hit'] + 1;
		$where = "id = '$id'";
		$data = array ('hit' => $hit );		
		$this->db->update ( 'pro_details', $data, $where );
	}
	public function GetProductInfo($id){
		$sql = 'select * from pro_details where id="'.$id.'"';
		return $this->db->fetchRow($sql);
	}
	public function deleteProduct($id){		
		$info = $this->GetProductInfo($id);
		$this->DeleteImageOnHost('product/', $info['image']);
		$where = "id='".$id."'";
		$this->db->delete('pro_details',$where);
	}
	///manufact
	public function GetAllProduct_manufact(){
		$sql = 'select * from pro_manufact';
		return $this->db->fetchAll($sql);
	}
	public function GetProduct_manufactInfo($id){
		$sql = 'select * from pro_manufact where id="'.$id.'"';
		return $this->db->fetchRow($sql);
	}
	///type
	public function GetAllProduct_type(){
		$sql = 'select * from pro_type';
		return $this->db->fetchAll($sql);
	}
	public function GetAllProduct_typeByStore($store){
		$sql = 'select * from pro_type where store='.$store;
		return $this->db->fetchAll($sql);
	}
	
	public function GetProduct_type($id){
		$sql = 'select * from pro_type where id="'.$id.'"';
		return $this->db->fetchRow($sql);
	}
	
	
	//count
	public function CountCatInType($type)
	{
		$sql = 'select * from pro_cat where type="'.$type.'"';
		return count($this->db->fetchAll($sql));
	}
	public function CountTypeInStore($store)
	{
		$sql = 'select * from pro_type where store="'.$store.'"';
		return count($this->db->fetchAll($sql));
	}
	public function CountProInCat($cat){
		$sql = 'select * from pro_details where cat="'.$cat.'"';
		return count($this->db->fetchAll($sql));
	}
	public function CountProInType($type){
		$sql = 'select pro_details.* from pro_details,pro_cat where pro_details.cat=pro_cat.id and pro_cat.type="'.$type.'"';
		return count($this->db->fetchAll($sql));
	}
	public function deleteType($id){		
		$info = $this->GetProduct_type($id);
		
		$this->DeleteImageOnHost('product/type', $info['image']);
		$this->deleteContentTagByContent($id);
		$where = "id='".$id."'";
		$this->db->delete('news_content',$where);
	}
//currency
	public function GetAllPro_currency(){
		$sql = 'select * from pro_currency';
		return $this->db->fetchAll($sql);
	}
	public function GetPro_curInfo($id){
		$sql = 'select * from pro_currency where id="'.$id.'"';
		return $this->db->fetchRow($sql);
	}
//store
	public function GetAllPro_store(){
		$sql = 'select * from pro_store';
		return $this->db->fetchAll($sql);
	}
	public function GetPro_storeInfo($id){
		$sql = 'select * from pro_store where id="'.$id.'"';
		return $this->db->fetchRow($sql);
	}
	public function deleteStore($id){		
		$info = $this->GetPro_storeInfo($id);
		$this->DeleteImageOnHost('product/store/', $info['image']);
		$where = "id='".$id."'";
		$this->db->delete('pro_store',$where);
	}
	public function CountProInStore($store)
	{
		$sql = 'select * from pro_details where store="'.$store.'"';
		return count($this->db->fetchAll($sql));
	}
	public function CountCurencyInStore($curency)
	{
		$sql = 'select * from pro_store where curency="'.$curency.'"';
		return count($this->db->fetchAll($sql));
	}
	public function CountProInManufact($manufact)
	{
		$sql = 'select * from pro_details where manufact="'.$manufact.'"';
		return count($this->db->fetchAll($sql));
	}

//cat

	public function GetCatByType($type){
		$sql = 'select * from pro_cat where type="'.$type.'"';
		return $this->db->fetchRow($sql);
	}
	public function GetAllPro_cat(){
		$sql = 'select * from pro_cat';
		return $this->db->fetchAll($sql);
	}
	public function GetAllPro_catByTye($type){
		$sql = 'select * from pro_cat where type='.$type;
		return $this->db->fetchAll($sql);
	}
	public function GetPro_catInfo($id){
		$sql = 'select * from pro_cat where id="'.$id.'"';
		return $this->db->fetchRow($sql);
	}	
	public function deleteCat($id){		
		$info = $this->GetPro_catInfo($id);
		$this->DeleteImageOnHost('product/cat/', $info['image']);
		$where = "id='".$id."'";
		$this->db->delete('pro_cat',$where);
	}

	public function delPro_manufact($id) {
		$info = $this->GetProduct_manufactInfo($id);
		$this->DeleteImageOnHost('product/manufact/', $info['image']);
		$where = "id =  '$id'";
		$this->db->delete ( 'pro_manufact', $where );
	}
	public function delPro_type($id) {
		$info = $this->GetProduct_type($id);
		$this->DeleteImageOnHost('product/type/', $info['image']);
		$where = "id =  '$id'";
		$this->db->delete ( 'pro_type', $where );
	}
	///               tag          ///////////
	public function GetTagname($id){
		$sql = 'select name from pro_tag where id='.$id;
		$result =  $this->db->fetchRow($sql);
		return $result['name'];
	}
	public function GetContentByTag($tag){
		$sql = 'select content from pro_tag_pro where tag='.$tag;
		return $this->db->fetchAll($sql);
	}
	public function GetTagByContent($product){
		$sql = 'select tag from pro_tag_pro where product='.$product;
		return $this->db->fetchAll($sql);
	}
}